/**
 * Created by qinj on 2015/5/18.
 * 渠道营销-全民经纪人
 */
define(function (require, exports, module) {
    require("angular-ui");
    require("authorize");
    require("angular-route");
    require("permission");
    var angular = require("angular");
    require("angular-interceptor");
    require("pagination");
    require('./member');
    require('./team');
    require('./activity');
    require('./param');
    require('./feedback');

    require('/static_public/js/angular_directive/projectTreeSelector');
    
    angular.module('app', ['ngRoute', 'ngInterceptor', 'ngUI','projectTreeSelector','beeMember', 'beeTeam', 'beeActivity', 'beeParam'])
        .config(['$routeProvider', function ($routeProvider) {
            $routeProvider
                .when('/activity', { templateUrl: 'activity/index.html', controller: "ActivityController" })
                .when('/team', { templateUrl: 'team/index.html', controller: "TeamController" })
                .when('/member', { templateUrl: 'member/index.html', controller: "MemberController" })
                .when('/param', { templateUrl: 'param/index.html', controller: "ParamController" })
                .otherwise({ redirectTo: '/activity' });
        }])
        .controller('MainController', ['$rootScope', '$scope', '$location', '$route', '$routeParams', '$http', '$window',
            function ($rootScope, $scope, $location, $route, $routeParams, $http, $window) {
                $scope.tabs = [
                    {value: '/activity', name: '活动管理',needProj: true},
                    { value: '/team', name: '团队管理', needProj: true},
                    { value: '/member', name: '成员管理', needProj: false},
                    {value: '/param', name: '参数设置',needProj: false}
                ];

                $scope.current = $location.path();

                $scope.$watch('current', function (val) {
                    angular.forEach($scope.tabs, function (tab) {
                        if (tab.value === val) {
                            $scope.currentTab = tab;
                            return false;
                        }
                    })
                });

                $http.get('/api/index.php?r=xmf/xmf-team/project-list&flag=0&jointparent=1')
                    .then(function (res) {
                        if (res.data.retCode == "0") {
                            $scope.projectList = res.data.data;
                            // $scope.curProjectId = $scope.projectList[0].p_projectId;
                            // $scope.projectChanged();
                        }
                    });

                $scope.projectChanged = function () {
                    $scope.$broadcast('projChange', $scope.curProjectId);
                }

                $scope.$watch('curProjectId', function () {
                    if ($scope.curProjectId) {
                        $scope.projectChanged();
                    }
                })

                $rootScope.$on('$routeChangeSuccess', function (evt, cur, pre) {
                    $scope.current = $location.path();
                });
                
               
            }
        ]);

    angular.bootstrap(document, ['app']);
});
